import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import fs from 'fs';
import path from 'path';

// Load backend PORT from victory.env so proxy stays in sync
const victoryEnvPath = path.resolve(process.cwd(), 'victory.env');
if (fs.existsSync(victoryEnvPath)) {
  const content = fs.readFileSync(victoryEnvPath, 'utf-8');
  for (const line of content.split(/\r?\n/)) {
    const m = line.match(/^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/);
    if (m && !(m[1] in process.env)) {
      process.env[m[1]] = m[2];
    }
  }
}
const apiPort = Number(process.env.PORT || 8787);

export default defineConfig({
  plugins: [react()],
  server: {
    port: 5173,
    host: true,
    proxy: {
      '/api': {
        target: `http://localhost:${apiPort}`,
        changeOrigin: true
      }
    }
  }
});


